<?php

/* 日期 */

namespace Duante\GuLu;

use Duante\GuLu\calendar\CalendarInterface;

class Calendar implements CalendarInterface
{
    /**
     * 获取当前脚本时区
     * @return string
     */
    public static function timezone()
    {
        return date_default_timezone_get();
    }

    /**
     * 今天开始-结束时间戳
     * @return array
     */
    public static function today()
    {
        return [
            mktime(0, 0, 0, date('m'), date('d'), date('Y')),
            mktime(23, 59, 59, date('m'), date('d'), date('Y'))
        ];
    }

    /**
     * 昨天开始-结束时间戳
     * @return array
     */
    public static function yesterday()
    {
        $yesterday = date('d') - 1;
        return [
            mktime(0, 0, 0, date('m'), $yesterday, date('Y')),
            mktime(23, 59, 59, date('m'), $yesterday, date('Y'))
        ];
    }

    /**
     * 本周开始-结束时间戳
     * @return array
     */
    public static function week()
    {
        return [
            mktime(0, 0, 0, date('m'), date('d') - date('w') + 1, date('Y')),
            mktime(23, 59, 59, date('m'), date('d') - date('w') + 7, date('Y'))
        ];
    }

    /**
     * 上周开始-结束时间戳
     * @return array
     */
    public static function lastWeek()
    {
        $timestamp = time();
        return [
            strtotime(date('Y-m-d', strtotime("last week Monday", $timestamp))),
            strtotime(date('Y-m-d', strtotime("last week Sunday", $timestamp))) + 24 * 3600 - 1
        ];
    }

    /**
     * 本月开始-结束时间戳
     * @return array
     */
    public static function month()
    {
        return [
            mktime(0, 0, 0, date('m'), 1, date('Y')),
            mktime(23, 59, 59, date('m'), date('t'), date('Y'))
        ];
    }

    /**
     * 上个月开始-结束时间戳
     * @return array
     */
    public static function lastMonth()
    {
        $begin = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
        $end = mktime(23, 59, 59, date('m') - 1, date('t', $begin), date('Y'));

        return [$begin, $end];
    }

    /**
     * 今年开始-结束时间戳
     * @return array
     */
    public static function year()
    {
        return [
            mktime(0, 0, 0, 1, 1, date('Y')),
            mktime(23, 59, 59, 12, 31, date('Y'))
        ];
    }

    /**
     * 上一年开始-结束时间戳
     * @return array
     */
    public static function lastYear()
    {
        $year = date('Y') - 1;
        return [
            mktime(0, 0, 0, 1, 1, $year),
            mktime(23, 59, 59, 12, 31, $year)
        ];
    }

    /**
     * 天数转换为时间戳
     * @param int $day 天数
     * @return float|int
     */
    public static function daysToSec($day = 1)
    {
        return $day * 86400;
    }

    /**
     * 周转换为时间戳
     * @param int $week 周数
     * @return float|int
     */
    public static function weeksToSec($week = 1)
    {
        return self::daysToSec() * 7 * $week;
    }

    /**
     * 获取几天前零点到现在/昨天结束的时间戳
     * @param int $day 天数
     * @param bool $now 是否现在时间
     * @return array
     */
    public static function daysToNow($day = 1, $now = true)
    {
        $end = time();
        if (!$now) {
            list($foo, $end) = self::yesterday();
        }

        return [
            mktime(0, 0, 0, date('m'), date('d') - $day, date('Y')),
            $end
        ];
    }

    /**
     * 返回几天前的时间戳
     * @param int $day 天数
     * @return float|int
     */
    public static function daysAgo($day = 1)
    {
        $now = time();
        return $now - self::daysToSec($day);
    }

    /**
     * 返回几天后的时间戳
     * @param int $day 天数
     * @return float|int
     */
    public static function daysAfter($day = 1)
    {
        $nowTime = time();
        return $nowTime + self::daysToSec($day);
    }

    /**
     * 通过时间戳获取哪年的第几周 (年末年初有交叉的算是下一年的第一周)
     * @param $timestamp 时间戳
     * @return array
     */
    public static function getWeekByTimestamp($timestamp)
    {
        $week = date('W', $timestamp);
        $month = date('m', $timestamp);
        $year = date('Y', $timestamp);

        if (1 == $week && 12 == $month) {
            $year += 1;
        }

        return array(
            'Y' => $year,
            'W' => $week
        );
    }

    /**
     * 判断2个时间戳是否属于同一周
     * @param $t1 时间戳1
     * @param $t2 时间戳2
     * @return bool
     */
    public static function isSameWeek($t1, $t2)
    {
        $week1 = self::getWeekByTimestamp($t1);
        $week2 = self::getWeekByTimestamp($t2);

        return ($week1['Y'] == $week2['Y'] && $week1['W'] == $week2['W']) ? true : false;
    }

}